﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VisualExamEngine
{
    public partial class frmQuestionType : Form
    {
        private frmMain MainForm;

        public frmQuestionType(frmMain MainForm)
        {
            InitializeComponent();

            this.MainForm = MainForm;
        }

        /// <summary>
        /// <summary>
        /// DateTime: 5/7/15 11:50 PM
        /// Author: Mike Hartwig
        /// Purpose: Close the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// DateTime: 5/7/15 11:50 PM
        /// Author: Mike Hartwig
        /// Purpose: Add the question
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            TreeView trvExamExplorer = MainForm.ActiveMdiChild.Controls.Find("trvExamExplorer", true).FirstOrDefault() as TreeView;

            if (trvExamExplorer.SelectedNode != null & trvExamExplorer.SelectedNode.Level == 0)
            {
                clsQuestion question = new clsQuestion((clsExam) trvExamExplorer.SelectedNode.Tag);
                question.Number = trvExamExplorer.SelectedNode.Nodes.Count + 1;
                question.Type = this.cmbQuestionType.SelectedItem.ToString();
                question.SelectedSection = this.cmbSections.SelectedIndex;

                TreeNode aNode = new TreeNode();
                aNode.Text = "Question " + (trvExamExplorer.SelectedNode.Nodes.Count + 1).ToString();
                aNode.Tag = question;

                //add the question to the exam object
                clsExam currentExam = (clsExam)MainForm.ActiveMdiChild.Tag;

                //add the question
                currentExam.Questions.Add(question);

                trvExamExplorer.SelectedNode.Nodes.Add(aNode);

                ListView lstQuestions = MainForm.ActiveMdiChild.Controls.Find("lstQuestionListing", true).FirstOrDefault() as ListView;

                lstQuestions.Items.Clear();

                //show all questions in the listview
                foreach (TreeNode node in trvExamExplorer.SelectedNode.Nodes)
                {
                    ListViewItem qItem = new ListViewItem();

                    clsQuestion q = (clsQuestion) node.Tag;
                    qItem.Text = node.Text;
                    
                    qItem.SubItems.Add(q.Type);


                    if (q.Section.Count > 0)
                    {
                        qItem.SubItems.Add(q.Section[q.SelectedSection]);
                    }
                    else
                    {
                        qItem.SubItems.Add("(none)");
                    }
                    
                    lstQuestions.Items.Add(qItem);
                }

                this.Close();
            }
        }

        /// <summary>
        /// DateTime: 5/13/15 11:50 PM
        /// Author: Mike Hartwig
        /// Purpose: Form load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmQuestionType_Load(object sender, EventArgs e)
        {
            //get all the sections from the exam
            clsExam exam = (clsExam)MainForm.ActiveMdiChild.Tag;

            //add the default section
            cmbSections.Items.Add("(none)");

            if (exam.Sections != null)
            {
                foreach (string str in exam.Sections)
                {
                    cmbSections.Items.Add(str);
                }
            }

            this.btnOK.Enabled = false;
        }

        /// <summary>
        /// DateTime: 5/14/15 11:50 PM
        /// Author: Mike Hartwig
        /// Purpose: Make sure both combo's have values before we let user click ok
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbQuestionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbSections.SelectedItem != null)
            {
                this.btnOK.Enabled = true;
            }
            else
            {
                this.btnOK.Enabled = false;
            }
        }

        /// <summary>
        /// DateTime: 5/14/15 11:50 PM
        /// Author: Mike Hartwig
        /// Purpose: Make sure both combo's have values before we let user click ok
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSections_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbQuestionType.SelectedItem != null)
            {
                this.btnOK.Enabled = true;
            }
            else
            {
                this.btnOK.Enabled = false;
            }
        }
    }
}
